/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.Teachchapter;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.TeachchapterService;
import me.zhengjie.modules.system.domain.vo.TeachchapterQueryCriteria;
import me.zhengjie.modules.system.mapper.TeachchapterMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author shitou
* @date 2024-04-21
**/
@Service
@RequiredArgsConstructor
public class TeachchapterServiceImpl extends ServiceImpl<TeachchapterMapper, Teachchapter> implements TeachchapterService {

    private final TeachchapterMapper teachchapterMapper;

    private final UserMapper userMapper;

    @Override
    public PageResult<Teachchapter> queryAll(TeachchapterQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(teachchapterMapper.findAll(criteria, page));
    }

    @Override
    public List<Teachchapter> queryAll(TeachchapterQueryCriteria criteria){
        return teachchapterMapper.findAll(criteria);
    }

    // 查询章节列表

    @Transactional(rollbackFor = Exception.class)
    public List<Teachchapter> queryChapterList(Integer teachid){
        return teachchapterMapper.queryChapterList(teachid);
    }

    @Override
    public Teachchapter queryTeachchapterMsg(Integer teachchapterid) {
        return teachchapterMapper.selectById(teachchapterid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(Teachchapter resources) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);
        resources.setUserid(currentUser.getId());
        resources.setLastupdatetime(new Date().getTime());

        save(resources);
        return resources.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long update(Teachchapter resources) {
        Teachchapter teachchapter = getById(resources.getId());

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);
        resources.setUserid(currentUser.getId());
        resources.setLastupdatetime(new Date().getTime());

        teachchapter.copy(resources);
        saveOrUpdate(teachchapter);
        return teachchapter.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Teachchapter> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Teachchapter teachchapter : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("数字教材id", teachchapter.getTeachid());
            map.put("名称", teachchapter.getName());
            map.put("状态", teachchapter.getState());
            map.put("排序", teachchapter.getOrderby());
            map.put("创建用户", teachchapter.getUserid());
            map.put("创建时间", teachchapter.getCreatetime());
            map.put("操作时间", teachchapter.getLastupdatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}